package top.infopub.mgr.sts.service.impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.sts.dao.ProProfitPoolDao;
import top.infopub.mgr.sts.domain.ProProfitPoolDomain;
import top.infopub.mgr.sts.service.ProProfitPoolService;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.sts.ProProfitPool;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;


@Service
public class ProProfitPoolServiceImpl extends GenericServiceImpl<ProProfitPool, String> implements ProProfitPoolService {

    @Autowired
    private ProProfitPoolDao proProfitPoolDao;

    @Override
    public GenericDao<ProProfitPool, String> getDao() {
        return proProfitPoolDao;
    }

    @Override
    public Page<ProProfitPool> selectQaDataByWhere(Page<ProProfitPool> page, String proCode) {
        Map<String, Object> where = Maps.newHashMap();
        where.put("proCode", proCode);
        where.put("orderCondition", "STS_MONTH desc");
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProProfitPool>)proProfitPoolDao.selectByMapWhere(where);
    }

    @Override
    public BigDecimal selectTotalProfitAmount(String proCode) {
        return proProfitPoolDao.selectTotalProfitAmount(proCode);
    }

    @Override
    public Page<ProProfitPoolDomain> selectDataByWhere(Date monthStart, Date monthEnd,
                                                       String type, String orgId,
                                                       String orgProName,
                                                       Page<ProProfitPoolDomain> page) {
        Map<String, Object> where = Maps.newHashMap();
        where.put("stsMonthStart", monthStart);
        Date stsMonthEnd = DateUtils.getMonthEnd(monthEnd);
        where.put("stsMonthEnd", stsMonthEnd);
        if (type.equals("2")) {//区域公司
            where.put("hrOrgAreaId", orgId);
        }
        else if (type.equals("3")) {//项目公司
            where.put("hrOrgProId", orgId);
        }
        where.put("proName", orgProName);
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<ProProfitPoolDomain> proDomains = proProfitPoolDao.selectDataByWhere(where);
        for (ProProfitPoolDomain proProfitPoolDomain : proDomains) {
            BigDecimal totalSumTts = proProfitPoolDomain.getTotalSumTts();
            BigDecimal totalSumHms = totalSumTts.divide(new BigDecimal(10000)).setScale(6, BigDecimal.ROUND_HALF_UP);
            BigDecimal caeGrossProfitTts = proProfitPoolDomain.getCaeGrossProfitTts();
            BigDecimal caeGrossProfitHms = caeGrossProfitTts.divide(new BigDecimal(10000)).setScale(6, BigDecimal.ROUND_HALF_UP);
            BigDecimal effectFeeTts = proProfitPoolDomain.getEffectFeeTts();
            BigDecimal effectFeeHms = effectFeeTts.divide(new BigDecimal(10000)).setScale(6, BigDecimal.ROUND_HALF_UP);
            BigDecimal finSpreadTts = proProfitPoolDomain.getFinSpreadTts();
            BigDecimal finSpreadHms = finSpreadTts.divide(new BigDecimal(10000)).setScale(6, BigDecimal.ROUND_HALF_UP);
            BigDecimal manageFeeTts = proProfitPoolDomain.getManageFeeTts();
            BigDecimal manageFeeHms = manageFeeTts.divide(new BigDecimal(10000)).setScale(6, BigDecimal.ROUND_HALF_UP);
            BigDecimal fiaFeeTts = proProfitPoolDomain.getFiaFeeTts();
            BigDecimal fiaFeeHms = fiaFeeTts.divide(new BigDecimal(10000)).setScale(6, BigDecimal.ROUND_HALF_UP);
            BigDecimal taxFeeTts = proProfitPoolDomain.getTaxFeeTts();
            BigDecimal taxFeeHms = taxFeeTts.divide(new BigDecimal(10000)).setScale(6, BigDecimal.ROUND_HALF_UP);
            BigDecimal totalSumProfitTts = proProfitPoolDomain.getTotalSumProfitTts();
            BigDecimal totalSumProfitHms = totalSumProfitTts.divide(new BigDecimal(10000)).setScale(6, BigDecimal.ROUND_HALF_UP);
            proProfitPoolDomain.setTotalSumHms(totalSumHms);
            proProfitPoolDomain.setCaeGrossProfitHms(caeGrossProfitHms);
            proProfitPoolDomain.setEffectFeeHms(effectFeeHms);
            proProfitPoolDomain.setFinSpreadHms(finSpreadHms);
            proProfitPoolDomain.setManageFeeHms(manageFeeHms);
            proProfitPoolDomain.setFiaFeeHms(fiaFeeHms);
            proProfitPoolDomain.setTaxFeeHms(taxFeeHms);
            proProfitPoolDomain.setTotalSumProfitHms(totalSumProfitHms);
        }
        return (Page<ProProfitPoolDomain>)proDomains;
    }
}